using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace OA.BLL
{
    public class Manager : Base
    {

        private DAL.Manager dal = null;
        private DAL.ManagerGroup groupdal = null;

        public Manager()
        {
            isession = DB_help.NHibernateHelper.GetCurrentSession(DefaultDataBase);
            dal = new DAL.Manager(isession);
            groupdal = new DAL.ManagerGroup(isession);
        }

        public Common.Result Add(Model.Manager value)
        {
            Common.Result ret = new Common.Result(false, 0, "添加失败");
            if (dal.GetModel(value.UserName) != null)
            {
                ret.message = "[用户名]已经存在";
                return ret;
            }
            if (!string.IsNullOrEmpty(value.GroupID))
            {
                value.Group = groupdal.GetModel(value.GroupID);
                if (value.Group == null)
                {
                    ret.message = "[用户组]不存在";
                    return ret;
                }
            }
            value.Password = Common.Encrypt.MD5.MD5_32(value.Password);
            value.CreatTime = DateTime.Now;
            ret = dal.Add(value);
            return ret;
        }

        public Common.Result Update(Model.Manager value)
        {
            Common.Result ret = new Common.Result(false, 0, "修改失败");
            var dalvalue = dal.GetModel(value.UserName);
            if (dalvalue == null)
            {
                ret.success = false;
                ret.count = 0;
                ret.message = "数据不存在";
                return ret;
            }
            if (!string.IsNullOrEmpty(value.GroupID))
            {
                dalvalue.Group = groupdal.GetModel(value.GroupID);
                if (dalvalue.Group == null)
                {
                    ret.message = "用户组不存在";
                    return ret;
                }
            }
            dalvalue.ManagerType = value.ManagerType;
            if (!string.IsNullOrEmpty(value.Password)) dalvalue.Password = Common.Encrypt.MD5.MD5_32(value.Password);
            dalvalue.RealName = value.RealName;
            dalvalue.Telephone = value.Telephone;
            dalvalue.IsLock = value.IsLock;
            dalvalue.LockFailTime = value.LockFailTime;
            dalvalue.GroupID = value.GroupID;
            dalvalue.Email = value.Email;

            ret = dal.Update(dalvalue);
            return ret;
        }

        public Common.Result Delete(string ids)
        {
            isession.Clear();//清除检查权限产生缓存，要删除的数据如果在缓存中会导致异常
            var ret = new Common.Result(false, 0, null);
            foreach (var id in ids.Split(','))
            {
                LoginUser.RemoveOnlineManager(id);
            }
            ret = dal.Delete(ids);
            return ret;
        }

        public Model.Manager Get(string UserName)
        {
            var model = dal.GetModel(UserName);
            return model;
        }

        public IList<Model.Manager> Get(string where, string orderby, int start, int length, params object[] ps)
        {
            return dal.Get(where, orderby, start, length, ps);
        }

        public int GetCount(string where, params object[] ps)
        {
            return dal.GetCount(where, ps);
        }


        /// <summary>
        /// 验证用户账号
        /// </summary>
        public Common.Result ValidateUser(string userName, string password)
        {
            return ValidateUserByMd5(userName, Common.Encrypt.MD5.MD5_32(password));
        }

        /// <summary>
        /// 验证用户账号
        /// </summary>
        public Common.Result ValidateUserByMd5(string userName, string md5Password)
        {
            Common.Result ret = new Common.Result(false, 0, "用户名或密码错误");
            var mode = dal.GetModel(userName);
            if (mode == null)
            {
                ret.count = 2;
                return ret;
            }
            if (mode.Password != md5Password)
            {
                ret.count = 3;
                return ret;
            }
            if (mode.IsLock)
            {
                if (mode.LockFailTime != null && mode.LockFailTime <= DateTime.Now)
                {
                    mode.IsLock = false;
                    mode.LockFailTime = null;
                    dal.Update(mode);
                }
                else
                {
                    ret.count = 4;
                    ret.message = "用户已经被锁定";
                    return ret;
                }
            }
            ret.success = true;
            ret.message = "验证成功";
            ret.data = mode;
            return ret;
        }
    }
}
